<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                template="./../template/WorkspaceTemplate.xhtml">

    <ui:define name="top">

    </ui:define>

    <ui:define name="left">
    </ui:define>

    <ui:define name="right">

    </ui:define>

    <ui:define name="content">
        <style>
            .ui-state-highlight.ui-selected {
                color: blueviolet;
            }
        </style>
        <h2>Update Credit Group (Direct Sales)</h2>

        <h:form id="creditGroupForm" style="font-size: 85%">
            <p:panel id="panel1">
                <p:messages />
                <br/>
                <h:outputText value="Direct Sales Credit Policy" style="font-weight: bold; font-size: 110%"/>
                <p:separator/> 
                <br/>
                <p:dataTable var="cg" value="#{cgManagerBean.allCreditGroup}">
                    <p:column headerText="Credit Group Title" sortBy="#{cg.creditGroupTitle}" filterBy="#{cg.creditGroupTitle}">    
                        <h:outputText value="#{cg.creditGroupTitle}" />  
                    </p:column>
                    <p:column headerText="Extension Day" sortBy="#{cg.extensionDays}" filterBy="#{cg.extensionDays}">    
                        <h:outputText value="#{cg.extensionDays}" />  
                    </p:column>
                    <p:column headerText="Credit Limit" sortBy="#{cg.creditLimitForDs}" filterBy="#{cg.creditLimitForDs}">    
                        <h:outputText value="#{cg.creditLimitForDs}" />  
                    </p:column>
                    <p:column headerText="Upgrade Limit" sortBy="#{cg.upgradeLimitForDs}" filterBy="#{cg.upgradeLimitForDs}">    
                        <h:outputText value="#{cg.upgradeLimitForDs}" />  
                    </p:column>
                    <p:column headerText="Options">
                        <p:commandButton image="ui-icon ui-icon-pencil" actionListener="#{cgManagerBean.updateCreditGroup}" oncomplete="creditGroupDialog.show()" update="creditGroupUpdateForm">
                            <f:attribute value="#{cg}" name="selectedCreditGroup" />
                        </p:commandButton>
                    </p:column>
                </p:dataTable>


                <br/>
                <br/>
                <br/>
                <div align="right">
                    <p:commandButton value="Back to View Credit Group" image="ui-icon ui-icon-arrowreturnthick-1-w" action="manageCreditGroup?faces-redirect=true" update="creditGroupUpdateForm"/>
                </div>
                <br/>
                <br/>
                <br/>

            </p:panel>

        </h:form>
        
        <p:dialog id="creditGroupDialog" header="Update Credit Group" width="600" widgetVar="creditGroupDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false" closable="false">
            <h:form id="creditGroupUpdateForm" style="font-size: 85%">
                <p:messages/>
                <br/>
                <h:panelGrid columns="2" cellpadding="10">
                    <h:outputLabel value="Credit Group Title: " style="font-weight: bold"/>
                    <h:outputText value="#{cgManagerBean.cg.creditGroupTitle}" />
                    <h:outputLabel value="Extension Days: " style="font-weight: bold"/>
                    <h:outputText value="#{cgManagerBean.cg.extensionDays}" />
                    <h:outputLabel value="Credit Limit for Direct Sales: " style="font-weight: bold"/>
                    <h:inputText value="#{cgManagerBean.cg.creditLimitForDs}" required="true" requiredMessage="Credit Limit for Direct Sales cannot be empty"/>
                    <h:outputLabel value="Upgrade Limit for Direct Sales: " style="font-weight: bold"/>
                    <h:inputText value="#{cgManagerBean.cg.upgradeLimitForDs}" required="true" disabled="#{cgManagerBean.cg.upgradeLimitForDs == 0}" requiredMessage="Upgrade Limit for Direct Sales cannot be empty"/>
                </h:panelGrid>
                <br/>
                <br/>
                <div align="right">
                    <p:commandButton value="Cancel" image="ui-icon ui-icon-close" oncomplete="creditGroupDialog.hide()" update="creditGroupForm"/>
                    <p:commandButton value="Update" image="ui-icon ui-icon-disk" oncomplete="handleUpdateCreditGroup(xhr, status, args)" action="#{cgManagerBean.saveCreditGroup()}" update="creditGroupForm creditGroupUpdateForm"/>
                </div>
            </h:form>
        </p:dialog>

        <script type="text/javascript">  
            function handleUpdateCreditGroup(xhr, status, args) {  
                if(!args.valid) {  
                    jQuery('#creditGroupDialog').parent().effect("shake", { times:3 }, 100);
                } else {  
                    creditGroupDialog.hide();
                }  
            }  
        </script>

    </ui:define>

    <ui:define name="bottom">

    </ui:define>

</ui:composition>
